package com.clannad.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clannad.eduservice.entity.EduChapter;
import com.clannad.eduservice.entity.EduCourse;
import com.clannad.eduservice.entity.EduCourseDescription;
import com.clannad.eduservice.entity.EduVideo;
import com.clannad.eduservice.entity.frontVo.CourseFrontVo;
import com.clannad.eduservice.entity.frontVo.CourseWebVo;
import com.clannad.eduservice.entity.vo.CourseInfoVo;
import com.clannad.eduservice.entity.vo.CoursePublishVo;
import com.clannad.eduservice.mapper.EduCourseMapper;
import com.clannad.eduservice.service.EduChapterService;
import com.clannad.eduservice.service.EduCourseDescriptionService;
import com.clannad.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clannad.eduservice.service.EduVideoService;
import com.clannad.servicebase.exceptionhandler.GuliException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.cache.annotation.Cacheable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author clannad
 * @since 2022-08-11
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;

    @Override
    @Transactional
    @CacheEvict(value = "course",allEntries = true)
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        boolean save = this.save(eduCourse);
        if(!save){
            //添加成功
            throw  new GuliException(20001,"添加课程信息失败");
        }

        String cid=eduCourse.getId();
        EduCourseDescription description=new EduCourseDescription();
        description.setDescription(courseInfoVo.getDescription());
        description.setId(cid);
        eduCourseDescriptionService.save(description);

        return cid;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        CourseInfoVo courseInfoVo=new CourseInfoVo();
        EduCourse eduCourse = this.getById(courseId);
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(courseId);
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        courseInfoVo.setDescription(eduCourseDescription.getDescription());
        return courseInfoVo;
    }

    @Override
    @Transactional
    @CacheEvict(value = "course",allEntries = true)
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        boolean b = this.updateById(eduCourse);
        if(!b){
            throw new GuliException(20001,"更新课程信息失败");
        }

        EduCourseDescription eduCourseDescription=new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(courseInfoVo.getId());
        eduCourseDescriptionService.updateById(eduCourseDescription);

    }

    @Override
    public CoursePublishVo getPublishCourseInfo(String id) {
        CoursePublishVo publishCourseInfo = this.baseMapper.getPublishCourseInfo(id);
        return publishCourseInfo;
    }

    @Override
    @Transactional
    @CacheEvict(value = "course",allEntries = true)
    public void removeCourse(String courseId) {
        //删除小节
        eduVideoService.removeVideoByCourseId(courseId);
        //删除章节
        eduChapterService.removeChapterBybCourseID(courseId);
        //删除课程
        boolean b = this.removeById(courseId);
        if(!b){
            throw new GuliException(20001,"删除失败");
        }
    }

    @Override
    @Cacheable(value = "course",key = "'course'")
    public List<EduCourse> getCourseIndex() {
        LambdaQueryWrapper<EduCourse> courseLambdaQueryWrapper=new LambdaQueryWrapper<>();
        courseLambdaQueryWrapper.orderByDesc(EduCourse::getViewCount);
        courseLambdaQueryWrapper.last("limit 8");
        List<EduCourse> list = this.list(courseLambdaQueryWrapper);
        return list;
    }

    @Override
    public Map<String, Object> getCourseForntList(Page<EduCourse> pageCourse, CourseFrontVo courseFrontVo) {
        LambdaQueryWrapper<EduCourse> lambdaQueryWrapper=new LambdaQueryWrapper<>();

        if(courseFrontVo!=null){
            if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())){
                lambdaQueryWrapper.eq(EduCourse::getSubjectParentId,courseFrontVo.getSubjectParentId());
            }
            if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())){
                lambdaQueryWrapper.eq(EduCourse::getSubjectId,courseFrontVo.getSubjectId());
            }
            if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())){
                lambdaQueryWrapper.orderByDesc(EduCourse::getBuyCount);
            }
            if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())){
                lambdaQueryWrapper.orderByDesc(EduCourse::getGmtCreate);
            }
            if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())){
                lambdaQueryWrapper.orderByDesc(EduCourse::getPrice);
            }
        }
        this.page(pageCourse,lambdaQueryWrapper);
        List<EduCourse> records = pageCourse.getRecords();
        long current = pageCourse.getCurrent();
        long pages = pageCourse.getPages();
        long size = pageCourse.getSize();
        long total = pageCourse.getTotal();
        boolean hasNext = pageCourse.hasNext();
        boolean hasPrevious = pageCourse.hasPrevious();
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        EduCourse byId = this.getById(courseId);
        Long viewCount = byId.getViewCount();
        viewCount=viewCount+1;
        EduCourse course=new EduCourse();
        BeanUtils.copyProperties(byId,course);
        course.setViewCount(viewCount);
        this.updateById(course);
        return baseMapper.getBaseCourseInfo(courseId);
    }
}
